/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.format.DateUtils;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.feiyue.basic.server.net.NovelInfo;
import com.feiyue.basic.server.parser.Comment;
import com.feiyue.basic.server.parser.OneComment;
import com.feiyue.helper.Thread2ReportComments;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class BookCommentActivity extends Activity implements
		View.OnTouchListener, GestureDetector.OnGestureListener {
	private static final String[] SCORE = { "请选择评分", "1星 不好看", "2星 没感觉",
			"3星 一般般", "4星 还不错", "5星 很好看" };
	private int bookid;
	private TextView commentCountTextview;
	private PullToRefreshScrollView comments_scrollview;
	private int commentslength = 0;
	private LinearLayout commentslinearLayout;
	private Context context;
	private EditText editText;
	protected CharSequence firstlabel = "加载更多评论";
	private ImageView goback;
	protected InputMethodManager imm;
	private GestureDetector mGestureDetector;
	private int minVelocity = 150;
	private TextView profile_header_title;
	protected CharSequence secondlabel = "松开立即加载、往回拉则取消";
	private Spinner spinnerscore;
	private Button submitButton;
	protected CharSequence thirdlabel = "正在加载更多评论";
	private int verticalMinDistance = 200;

	private PullToRefreshBase.OnRefreshListener<ScrollView> getonsrollviewListener() {
		return new PullToRefreshBase.OnRefreshListener<ScrollView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				String str = DateUtils.formatDateTime(
						BookCommentActivity.this.getApplicationContext(),
						System.currentTimeMillis(), 524305);
				refreshView.getLoadingLayoutProxy()
						.setLastUpdatedLabel(str);
				new BookCommentActivity.GetDataTask().execute(new Void[0]);
			}
		};
	}

	private View.OnClickListener getsubmitonclickListener() {
		return new View.OnClickListener() {
			public void onClick(View paramView) {
				int i = (int) BookCommentActivity.this.spinnerscore
						.getSelectedItemId();
				if (i == 0) {
					Toast.makeText(BookCommentActivity.this.context, "请选择评分", 0)
							.show();
					return;
				}
				String str = BookCommentActivity.this.editText.getText()
						.toString();
				if ((str == null) || (str.equals(""))) {
					Toast.makeText(BookCommentActivity.this.context, "请输入评论内容",
							0).show();
					return;
				}
				BookCommentActivity.this.imm.hideSoftInputFromWindow(
						BookCommentActivity.this.editText.getWindowToken(), 0);
				new Thread2ReportComments(BookCommentActivity.this.context,
						NovelInfo.getUrl2ReportComment(
								BookCommentActivity.this.bookid,
								str.replace(" ", "_"), i), new Handler())
						.start();
			}
		};
	}

	private void initiateSpinner() {
		this.spinnerscore = ((Spinner) findViewById(2131099835));
		int i = SCORE.length;
		ArrayList localArrayList = new ArrayList();
		for (int j = 0;; ++j) {
			if (j >= i) {
				ArrayAdapter localArrayAdapter = new ArrayAdapter(this,
						17367048, localArrayList);
				localArrayAdapter.setDropDownViewResource(17367049);
				this.spinnerscore.setAdapter(localArrayAdapter);
				return;
			}
			localArrayList.add(SCORE[j]);
		}
	}

	private void initiatescrollview() {
		this.comments_scrollview = ((PullToRefreshScrollView) findViewById(2131099831));
		this.comments_scrollview.setMode(PullToRefreshBase.Mode.BOTH);
		this.comments_scrollview.setAnimation(null);
		this.comments_scrollview.getLoadingLayoutProxy().setPullLabel(
				this.firstlabel);
		this.comments_scrollview.getLoadingLayoutProxy().setRefreshingLabel(
				this.thirdlabel);
		this.comments_scrollview.getLoadingLayoutProxy().setReleaseLabel(
				this.secondlabel);
//		this.comments_scrollview.getLoadingLayoutProxy()
//				.setTextColor(-16777216);
		this.comments_scrollview.setOnRefreshListener(getonsrollviewListener());
		String str = DateUtils.formatDateTime(getApplicationContext(),
				System.currentTimeMillis(), 524305);
		this.comments_scrollview.getLoadingLayoutProxy().setLastUpdatedLabel(
				str);
		new GetDataTask().execute(new Void[0]);
	}

	public boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {
		if (!(this.mGestureDetector.onTouchEvent(paramMotionEvent)))
			return super.dispatchTouchEvent(paramMotionEvent);
		return true;
	}

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(2130903061);
		this.context = getApplicationContext();
		this.bookid = getIntent().getExtras().getInt("bookid");
		this.mGestureDetector = new GestureDetector(this);
		LinearLayout localLinearLayout = (LinearLayout) findViewById(2131099830);
		localLinearLayout.setOnTouchListener(this);
		localLinearLayout.setLongClickable(true);
		this.profile_header_title = ((TextView) findViewById(2131099665));
		this.profile_header_title.setText("读者评论");
		this.submitButton = ((Button) findViewById(2131099836));
		this.editText = ((EditText) findViewById(2131099833));
		this.commentslinearLayout = ((LinearLayout) findViewById(2131099837));
		this.commentCountTextview = ((TextView) findViewById(2131099832));
		initiateSpinner();
		this.submitButton.setOnClickListener(getsubmitonclickListener());
		this.imm = ((InputMethodManager) getApplicationContext()
				.getSystemService("input_method"));
		this.editText = ((EditText) findViewById(2131099833));
		this.imm.hideSoftInputFromWindow(this.editText.getWindowToken(), 0);
		this.goback = ((ImageView) findViewById(2131099661));
		this.goback.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				BookCommentActivity.this.finish();
				BookCommentActivity.this.overridePendingTransition(2130968586,
						2130968589);
			}
		});
		initiatescrollview();
	}

	protected void onDestroy() {
		super.onDestroy();
	}

	public boolean onDown(MotionEvent paramMotionEvent) {
		return false;
	}

	public boolean onFling(MotionEvent paramMotionEvent1,
			MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
		float f = Math.abs(paramMotionEvent1.getX() - paramMotionEvent2.getX())
				/ Math.abs(paramMotionEvent1.getY() - paramMotionEvent2.getY());
		if ((paramMotionEvent1.getX() - paramMotionEvent2.getX() > 0.0F)
				&& (f - 2.5D > 0.0D)) {
			finish();
			overridePendingTransition(2130968587, 2130968588);
			return true;
		}
		if ((paramMotionEvent1.getX() - paramMotionEvent2.getX() < 0.0F)
				&& (f - 2.5D > 0.0D)) {
			finish();
			overridePendingTransition(2130968586, 2130968589);
			return true;
		}
		return false;
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if (paramInt == 4) {
			finish();
			overridePendingTransition(2130968586, 2130968589);
			return true;
		}
		return super.onKeyDown(paramInt, paramKeyEvent);
	}

	public void onLongPress(MotionEvent paramMotionEvent) {
	}

	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public boolean onScroll(MotionEvent paramMotionEvent1,
			MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
		return false;
	}

	public void onShowPress(MotionEvent paramMotionEvent) {
	}

	public boolean onSingleTapUp(MotionEvent paramMotionEvent) {
		return false;
	}

	public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
		return this.mGestureDetector.onTouchEvent(paramMotionEvent);
	}

	private class GetDataTask extends AsyncTask
    {

        private Comment tempComment;

        protected  Object doInBackground(Object aobj[])
        {
            return doInBackground((Void[])aobj);
        }

        protected  String[] doInBackground(Void avoid[])
        {
            tempComment = NovelInfo.getComments(context, NovelInfo.getUrl2Comment(bookid, commentslength, 10), bookid, commentslength, 10);
            return null;
        }

        protected void onPostExecute(Object obj)
        {
            onPostExecute((String[])obj);
        }

        protected void onPostExecute(String as[])
        {
            if (tempComment == null || tempComment.getList() == null || tempComment.getList().size() == 0)
            {
                if (commentslength == 0)
                    Toast.makeText(context, "本书还没有用户评论", 0).show();
                else
                    Toast.makeText(context, "评论已加载完", 0).show();
            } else
            {
                int i = tempComment.getList().size();
                BookCommentActivity bookcommentactivity = BookCommentActivity.this;
                bookcommentactivity.commentslength = i + bookcommentactivity.commentslength;
                Object aobj[] = new Object[1];
                aobj[0] = Integer.valueOf(tempComment.getCount());
                String s = String.format("本书共%d条评论", aobj);
                commentCountTextview.setText(s);
                int j = 0;
                while (j < i) 
                {
                    LinearLayout linearlayout = (LinearLayout)((LayoutInflater)getSystemService("layout_inflater")).inflate(0x7f030014, null);
                    TextView textview = (TextView)linearlayout.findViewById(0x7f0600b3);
                    TextView textview1 = (TextView)linearlayout.findViewById(0x7f0600b4);
                    TextView textview2 = (TextView)linearlayout.findViewById(0x7f0600b5);
                    textview.setText(((OneComment)tempComment.getList().get(j)).getIp());
                    textview1.setText(((OneComment)tempComment.getList().get(j)).getTime());
                    textview2.setText(((OneComment)tempComment.getList().get(j)).getInfo());
                    android.view.ViewGroup.LayoutParams layoutparams = new android.view.ViewGroup.LayoutParams(-1, -2);
                    commentslinearLayout.addView(linearlayout, layoutparams);
                    j++;
                }
            }
            comments_scrollview.onRefreshComplete();
            super.onPostExecute(as);
        }
    }
}